
#The following script replicates all exhibits in the main body of the following paper: 
#Hiaeshutter-Rice, Fabian Neuner and Stuart Soroka, "Cued by Culture: Political Imagery and Partisan Evaluations,
#forthcoming in Political Behavior (as of May 2021).

#This file is accompanied by 7 datasets, used and described below.

library(tm)
library(wordcloud)
library(boot)


#######
#Figure 1
rm(list=ls())
load("dem.fulltext.prewhitened.Rdata") #full-text responses about Democrats, pre-whitened
load("rep.fulltext.prewhitened.Rdata") #full-text responses about Republicans, pre-whitened
A <- rbind(rep,dem)
corpus <- VCorpus (VectorSource(A))
tdm <- TermDocumentMatrix(corpus, control=list(removePunctuation=TRUE, stopwords=TRUE, removeNumbers=TRUE, tolower=TRUE, bounds=list(local = c(3, Inf)))) 
term.matrix <- as.matrix(tdm)
colnames(term.matrix) <- c("Republican","Democratic")
rownames(term.matrix)
#pdf("figure1.pdf", width = 8, height = 6)
set.seed(11)
comparison.cloud(term.matrix,max.words=150,random.order=FALSE,colors=c("firebrick","dodgerblue"),title.size=1.3)
#dev.off()


#######
#Figure 2
rm(list=ls())
load("item.descriptives.Rdata") #descriptive data for ratings of objects and activities
#pdf("figure2.pdf", width = 9, height = 7)
par(mar=c(5.1,7.1,4.1,2.1))
plot(descriptives$mean_lr,descriptives$number,type="p",ann=F,axes=F,col="Black",xlim=c(0,100),pch=20)
axis(1)
axis(2,at=c(1:26),labels=descriptives$symbol,las=1)
title(xlab="Democratic  <----->  Republican")
text(80,3,"Ratings by Party", col="Black",pos=2)
abline(v=50,lty=2,col="gray")
segments(y0=1:26, x0 = descriptives$mean_lr, x1=descriptives$iqr1_lr, 1:26, lwd = 2, col = "grey")
segments(y0=1:26, x0 = descriptives$mean_lr, x1=descriptives$iqr3_lr, 1:26, lwd = 2, col = "grey")
points(descriptives$mean_lr,descriptives$number, pch = 20)
#dev.off()


#######
#Figure 4
rm(list=ls())
load("candidate.ratings.bytreatment.Rdata") #descriptives for candidate photo ratings (party and liberal-conservative)
#pdf("figure4.pdf", width = 6, height = 5)
par(mar=c(5.1,10.1,4.1,2.1))
plot(B$party.m.w, B$order, ylim=c(1,12), type="n", xlim=c(0,1), ann=F, axes=F)
B$order <- NA
B$order[B$condition=="Control_blue"] <- 11
B$order[B$condition=="Organic"] <- 10
B$order[B$condition=="Tattoo"] <- 9
B$order[B$condition=="Guns_old"] <- 8
B$order[B$condition=="NASCAR"] <- 7
arrows(B$party.m.w-2*B$party.ste.w,B$order,B$party.m.w+2*B$party.ste.w,B$order, angle=90,code=3, length=.03, col="gray")
points(B$party.m.w, B$order, pch=20, lwd=2)
B$order[B$condition=="Control_purple"] <- 5
B$order[B$condition=="Organic"] <- 4
B$order[B$condition=="Prius"] <- 3
B$order[B$condition=="Guns_new"] <- 2
B$order[B$condition=="Trucks"] <- 1
arrows(B$party.m.m-2*B$party.ste.m,B$order,B$party.m.m+2*B$party.ste.m,B$order, angle=90,code=3, length=.03, col="gray")
points(B$party.m.m, B$order, pch=20, lwd=2)
axis(1, at=c(0,.2,.4,.6,.8,1), labels=c(0,20,40,60,80,100))
axis(2, las=1, at=c(1:5), labels=c("Truck Dealership *", "Gun Store *", "Hybrid Car", "Organic Groceries", "Control"))
axis(2, las=1, at=c(7:11), labels=c("NASCAR *", "Gun Store *", "Tattoo Parlor", "Organic Groceries", "Control"))
axis(2, las=1, at=c(6,12), labels=c("Male Candidate", "Female Candidate"), tick=F, line=NA)
title(xlab="% Respondents Believing Candidate is a Democrat")
#dev.off()

#Figure 4: t-tests
rm(list=ls())
load("candidate.ratings.ind.data.Rdata") #full-text responses about Democrats, pre-whitened
t.test(A$party[A$candidate=="male"&A$condition=="Control_purple"],A$party[A$candidate=="male"&A$condition=="Trucks"])
t.test(A$party[A$candidate=="male"&A$condition=="Control_purple"],A$party[A$candidate=="male"&A$condition=="Guns_new"])
t.test(A$party[A$candidate=="male"&A$condition=="Control_purple"],A$party[A$candidate=="male"&A$condition=="Organic"])
t.test(A$party[A$candidate=="male"&A$condition=="Control_purple"],A$party[A$candidate=="male"&A$condition=="Prius"])
t.test(A$party[A$candidate=="female"&A$condition=="Control_blue"],A$party[A$candidate=="female"&A$condition=="NASCAR"])
t.test(A$party[A$candidate=="female"&A$condition=="Control_blue"],A$party[A$candidate=="female"&A$condition=="Guns_old"])
t.test(A$party[A$candidate=="female"&A$condition=="Control_blue"],A$party[A$candidate=="female"&A$condition=="Organic"])
t.test(A$party[A$candidate=="female"&A$condition=="Control_blue"],A$party[A$candidate=="female"&A$condition=="Tattoo"])


#######
#Figure 5
rm(list=ls())
load("survey1.full.Rdata") #Survey 1 data (used variables only)
A <- s[s$ftt==1,]
E <- c("Control", "Guns", "NASCAR", "Organic", "Tattoo")
E <- as.data.frame(E) ; colnames(E) <- c("condition")
E$nums <- 1:5
E$Baplm <- NA
E$Baplse <- NA
E$Bapcm <- NA
E$Bapcse <- NA
meanFunc <- function(x,i){mean(x[i],na.rm=T)}
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Control" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Control"] <- results$t0
E$Baplse[E$condition=="Control"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Guns" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Guns"] <- results$t0
E$Baplse[E$condition=="Guns"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="NASCAR" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="NASCAR"] <- results$t0
E$Baplse[E$condition=="NASCAR"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Organic" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Organic"] <- results$t0
E$Baplse[E$condition=="Organic"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Tattoo" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Tattoo"] <- results$t0
E$Baplse[E$condition=="Tattoo"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Control" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Control"] <- results$t0
E$Bapcse[E$condition=="Control"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Guns" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Guns"] <- results$t0
E$Bapcse[E$condition=="Guns"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="NASCAR" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="NASCAR"] <- results$t0
E$Bapcse[E$condition=="NASCAR"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Organic" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Organic"] <- results$t0
E$Bapcse[E$condition=="Organic"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Tattoo" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Tattoo"] <- results$t0
E$Bapcse[E$condition=="Tattoo"] <- sd(results$t)
#pdf(file="figure5.pdf")
par(mar = c(6, 6, 5.5, 6))
plot(x=E$Baplm, y=E$nums, col="dodgerblue", xlim = c(0,1), ylim = c(.8,6), xaxt = "n", xlab = "Republican <-- Candidate Evaluation --> Democrat", ylab = "",pch = 15, cex=2, las = 2, axes = F)
axis(2, at=1:5, labels=E$condition, las = 2, col = F)
axis(1, seq(0,1, by = .1), labels = seq(0,100, by = 10))
text(.36,5.4, "Republican Policy", col = "firebrick")
text(.76,5.4, "Democratic Policy", col = "dodgerblue")
points(x=E$Bapcm, y=E$nums, col="red", pch = 15, cex = 2, las = 2)
segments(E$Baplm + E$Baplse * 2, 1:5, E$Baplm - E$Baplse * 2, 1:5, lwd = 2, col = "gray")
arrows(E$Baplm + E$Baplse * 2, 1:5, E$Baplm - E$Baplse * 2, 1:5, lwd = 2, angle = 90, code = 3, length = 0.05, col = "gray")
segments(E$Bapcm + E$Bapcse * 2, 1:5, E$Bapcm - E$Bapcse* 2, 1:5, lwd = 2, col = "gray")
arrows(E$Bapcm + E$Bapcse * 2, 1:5, E$Bapcm - E$Bapcse * 2, 1:5, lwd = 2, angle = 90, code = 3, length = 0.05, col = "gray")
points(x=E$Bapcm, y=E$nums, col="firebrick", pch = 15, cex = 2, las = 2)
points(x=E$Baplm, y=E$nums, col="dodgerblue", pch = 15, cex = 2, las = 2)
#dev.off()


#######
#Figure 6 
rm(list=ls())
load("survey2.full.Rdata") #Survey 2 data (used variables only)
A <- s[s$ftt==1,]
E <- c("Control", "Guns", "Organic", "Prius", "Truck")
E <- as.data.frame(E) ; colnames(E) <- c("condition")
E$nums <- 1:5
E$Baplm <- NA
E$Baplse <- NA
E$Bapcm <- NA
E$Bapcse <- NA
meanFunc <- function(x,i){mean(x[i],na.rm=T)}
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Control" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Control"] <- results$t0
E$Baplse[E$condition=="Control"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Guns" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Guns"] <- results$t0
E$Baplse[E$condition=="Guns"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Prius" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Prius"] <- results$t0
E$Baplse[E$condition=="Prius"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Organic" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Organic"] <- results$t0
E$Baplse[E$condition=="Organic"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Truck" & A$partisan.cond==1],meanFunc,1000)
E$Baplm[E$condition=="Truck"] <- results$t0
E$Baplse[E$condition=="Truck"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Control" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Control"] <- results$t0
E$Bapcse[E$condition=="Control"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Guns" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Guns"] <- results$t0
E$Bapcse[E$condition=="Guns"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Prius" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Prius"] <- results$t0
E$Bapcse[E$condition=="Prius"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Organic" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Organic"] <- results$t0
E$Bapcse[E$condition=="Organic"] <- sd(results$t)
set.seed(11)
results <- boot(A$cand.party[A$picture.cond=="Truck" & A$partisan.cond==0],meanFunc,1000)
E$Bapcm[E$condition=="Truck"] <- results$t0
E$Bapcse[E$condition=="Truck"] <- sd(results$t)
#pdf(file='figure6.pdf')
par(mar = c(6, 6, 5.5, 6))
plot(x=E$Baplm, y=E$nums, col="dodgerblue", xlim = c(0,1), ylim = c(.8,6), xaxt = "n", xlab = "Republican <-- Candidate Evaluation --> Democrat", ylab = "",pch = 15, cex=2, las = 2, axes = F)
axis(2, at=1:5, labels=E$condition, las = 2, col = F)
axis(1, seq(0,1, by = .1), labels = seq(0,100, by = 10))
text(.36,5.4, "Republican Policy", col = "firebrick")
text(.76,5.4, "Democratic Policy", col = "dodgerblue")
points(x=E$Bapcm, y=E$nums, col="red", pch = 15, cex = 2, las = 2)
segments(E$Baplm + E$Baplse * 2, 1:5, E$Baplm - E$Baplse * 2, 1:5, lwd = 2, col = "gray")
arrows(E$Baplm + E$Baplse * 2, 1:5, E$Baplm - E$Baplse * 2, 1:5, lwd = 2, angle = 90, code = 3, length = 0.05, col = "gray")
segments(E$Bapcm + E$Bapcse * 2, 1:5, E$Bapcm - E$Bapcse* 2, 1:5, lwd = 2, col = "gray")
arrows(E$Bapcm + E$Bapcse * 2, 1:5, E$Bapcm - E$Bapcse * 2, 1:5, lwd = 2, angle = 90, code = 3, length = 0.05, col = "gray")
points(x=E$Bapcm, y=E$nums, col="firebrick", pch = 15, cex = 2, las = 2)
points(x=E$Baplm, y=E$nums, col="dodgerblue", pch = 15, cex = 2, las = 2)
#dev.off()

